package sixkey.utils;

/**
 * ClassName:ItScriptWorkerUtils
 * Package:sixkey.utils
 * Description
 *
 * @Author:@wenxueshi
 * @Create:2023/7/1 - 23:26
 * @Version:v1.0
 */

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.springframework.stereotype.Component;
import sixkey.domain.entity.Content;

import java.util.ArrayList;
import java.util.List;

/**
 * 爬取数据脚本
 */
@Component
public class ItScriptWorkerUtils {

    public List<Content> execute(){
        //设置浏览器驱动
        System.setProperty("webdriver.chrome.driver","D:\\chromeDriver\\chromedriver.exe");
        ChromeOptions chromeOptions = new ChromeOptions();
        //默认只允许本地连接，解决403报错问题
        chromeOptions.addArguments("--remote-allow-origins=*");
        //初始化web测试驱动
        ChromeDriver chromeDriver = new ChromeDriver(chromeOptions);
        //打开入口页（要爬取数据网页的url）
        String url = "http://www.itpub.net/";
        chromeDriver.get(url);
        try {
            //线程休眠5s,确保已经打开网页
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        //开始爬取数据(获取网页前十页最新消息)
        int count = 1;
        List<Content> contents = new ArrayList<>();
        while (count <= 10){
            //通过css选择器获取html
            List<WebElement> elements = chromeDriver.findElements(By.cssSelector("div.right-box h4 a"));
            elements.forEach(item ->{
                Content content = new Content();
                String title = item.getText();
                String src = item.getAttribute("href");
                content.setTitle(title);
                content.setUrl(src);
                contents.add(content);
            });

            //遇到下一页按钮时自动点击事件
            List<WebElement> pages = chromeDriver.findElements(By.cssSelector("div.page a"));
            WebElement nextPage = pages.get(pages.size() - 1);
            //触发下一页按钮单机事件
            nextPage.click();
            count++;
            try {
                //线程休眠5s,确保已经下一页
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        //关闭驱动
        chromeDriver.close();
        return contents;
    }
}
